package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.constant.WareConstant;
import com.atguigu.gulimall.member.dao.MemberLevelDao;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UsernameExistException;
import com.atguigu.gulimall.member.vo.UserLoginVo;
import com.atguigu.gulimall.member.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import org.springframework.util.ObjectUtils;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    private MemberLevelDao memberLevelDao;

    private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void register(UserRegisterVo userRegisterVo) {
        // todo 检查用户名和手机号 是否唯一 为了让controller感知异常，使用异常机制
        checkUsernameUnique(userRegisterVo.getUsername());
        checkPhoneUnique(userRegisterVo.getPhone());

        MemberEntity memberEntity = new MemberEntity();
        // 设置注册数据
        memberEntity.setUsername(userRegisterVo.getUsername());
        memberEntity.setNickname(userRegisterVo.getUsername());
        // 密码加密存储
        memberEntity.setPassword(encoder.encode(userRegisterVo.getPassword()));
        memberEntity.setMobile(userRegisterVo.getPhone());

        // 设置会员等级ID 默认为普通
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());

        //设置其他信息

        baseMapper.insert(memberEntity);
    }

    @Override
    public MemberEntity login(UserLoginVo loginVo) {
        String account = loginVo.getLoginAccount();
        String password = loginVo.getPassword();

        // 查询数据库
        MemberEntity memberEntity = baseMapper.selectOne(
                new QueryWrapper<MemberEntity>()
                        .eq("username", account)
                        .or()
                        .eq("mobile", account)
        );
        if (memberEntity != null) {
            // 检查密码
            boolean matches = encoder.matches(password, memberEntity.getPassword());
            if (!matches) {
                return null;
            }
        }
        return memberEntity;
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException {
        Integer count = baseMapper.selectCount(
                new QueryWrapper<MemberEntity>()
                        .eq("mobile", phone)
        );
        if (count > 0) {
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUsernameUnique(String username) throws UsernameExistException {
        Integer count = baseMapper.selectCount(
                new QueryWrapper<MemberEntity>()
                        .eq("username", username)
        );
        if (count > 0) {
            throw new UsernameExistException();
        }
    }
}